package com.thinkgem.jeesite.modules.gen.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gen.dao.GenProjectModuleMapper;
import com.thinkgem.jeesite.modules.gen.entity.GenProject;
import com.thinkgem.jeesite.modules.gen.entity.GenProjectModule;
import com.thinkgem.jeesite.modules.gen.service.GenProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目配置Controller
 *
 * @author kongxiangsheng
 * @version 2018-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/genProject")
@Slf4j
public class GenProjectController extends BaseController {
    @Autowired
    private GenProjectService genProjectService;

    @Autowired
    private GenProjectModuleMapper genProjectModuleMapper;

    @RequestMapping(value = {"test"})
    @ResponseBody
    public String test(String id) {
        log.info("id：" + id);

        // 测试selectByEntity
        GenProjectModule query = new GenProjectModule();
        query.setProjectId("1059422922367713282");
        List<GenProjectModule> list = genProjectModuleMapper.selectByEntity(query);

        log.info("list:" + list);

        GenProject entity = genProjectService.getById(id);
        log.info("entity.module:" + entity.getGenProjectModuleList());

        List<GenProjectModule> moduleList = entity.getGenProjectModuleList();

        log.info("size:" + moduleList.size());

        return "成功";
    }

    @RequiresPermissions("gen:genProject:view")
    @RequestMapping(value = {"list", ""})
    public String list(GenProject genProject, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<GenProject> page = new BasePage<GenProject>(request, response);
        genProjectService.page(page, new QueryWrapper<GenProject>(genProject));

        model.addAttribute("page", page);
        return "modules/gen/GenProject/list";
    }

    @RequiresPermissions("gen:genProject:view")
    @RequestMapping(value = "form")
    public String form(GenProject genProject, Model model) {
        if (StringUtils.isNotBlank(genProject.getId())) {
            genProject = genProjectService.getWithChildren(genProject.getId());
        }
        model.addAttribute("genProject", genProject);

        if(StringUtils.isEmpty(genProject.getBaseColumns())){
            genProject.setBaseColumns("id");
        }
        if(StringUtils.isEmpty(genProject.getBaseTreeColumns())){
            genProject.setBaseTreeColumns("id");
        }

        return "modules/gen/GenProject/form";
    }

    @RequiresPermissions("gen:genProject:edit")
    @RequestMapping(value = "save")
    public String save(GenProject genProject, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, genProject)) {
            return form(genProject, model);
        }
        genProjectService.saveWithChildren(genProject);
        addMessage(redirectAttributes, "保存项目配置成功");
        return "redirect:" + adminPath + "/gen/genProject/?repage";
    }

    @RequiresPermissions("gen:genProject:edit")
    @RequestMapping(value = "delete")
    public String delete(GenProject genProject, RedirectAttributes redirectAttributes) {
        genProjectService.deleteWithChildren(genProject);
        addMessage(redirectAttributes, "删除项目配置成功");
        return "redirect:" + adminPath + "/gen/genProject/?repage";
    }

}